package net.lab1024.sa.admin.module.business.erp.poo.service;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import net.lab1024.sa.admin.module.business.erp.poo.dao.PoordDao;
import net.lab1024.sa.admin.module.business.erp.poo.dao.PoordaDao;
import net.lab1024.sa.admin.module.business.erp.poo.dao.SubordaDao;
import net.lab1024.sa.admin.module.business.erp.poo.domain.entity.PoordEntity;
import net.lab1024.sa.admin.module.business.erp.poo.domain.entity.PoordaEntity;
import net.lab1024.sa.admin.module.business.erp.poo.domain.entity.SubordaEntity;
import net.lab1024.sa.admin.module.business.erp.poo.domain.form.PoordAddForm;
import net.lab1024.sa.admin.module.business.erp.poo.domain.form.PoordQueryForm;
import net.lab1024.sa.admin.module.business.erp.poo.domain.form.PoordUpdateForm;
import net.lab1024.sa.admin.module.business.erp.poo.domain.vo.PoordVO;
import net.lab1024.sa.admin.module.business.erp.poo.domain.vo.PoordaVO;
import net.lab1024.sa.base.common.code.ErpErrorCode;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 采购订单 Service
 *
 * @Author AKITA
 * @Date 2025-08-18 09:11:41
 * @Copyright AKITA
 */

@Service
public class PoordService {

    @Resource
    private PoordDao poordDao;
    @Resource
    private PoordaDao poordaDao;
    @Resource
    private SubordaDao subordaDao;

    public PageResult<PoordVO> queryPage(PoordQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<PoordVO> list = poordDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    @Transactional
    public ResponseDTO<Long> add(PoordAddForm addForm) {
        PoordEntity poordEntity = SmartBeanUtil.copy(addForm, PoordEntity.class);
        poordDao.insert(poordEntity);
        for (PoordaEntity poordaEntity : addForm.getLista()) {
            poordaEntity.setId(poordEntity.getId());
            poordaDao.insert(poordaEntity);
        }



        return ResponseDTO.ok(poordEntity.getId());
    }

    public ResponseDTO<String> update(PoordUpdateForm updateForm) {
        PoordEntity poordEntity = SmartBeanUtil.copy(updateForm, PoordEntity.class);
        poordDao.updateById(poordEntity);
        return ResponseDTO.ok();
    }

    public ResponseDTO<String> batchDelete(List<Long> idList) {
        poordDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }


    @Transactional

    public ResponseDTO<String> delete(Long id) {



        List<SubordaEntity> entities = subordaDao.selectList(new LambdaQueryWrapper<SubordaEntity>().eq(SubordaEntity::getBaseId, id));

        if (!entities.isEmpty()){
            return ResponseDTO.error(ErpErrorCode.PARAM_ERROR,"存在暂收送检单，禁止删除！");
        }


        poordDao.deleteById(id);
        poordaDao.deleteById(id);
        return ResponseDTO.ok();
    }

    public ResponseDTO<PoordVO> info(Long id) {
        PoordEntity poordEntity = poordDao.selectById(id);
        PoordVO copy = SmartBeanUtil.copy(poordEntity, PoordVO.class);
        List<PoordaEntity> poordaEntities = poordaDao.selectList(new LambdaQueryWrapper<PoordaEntity>().eq(PoordaEntity::getId, id));
        List<PoordaVO> poordaVOS = SmartBeanUtil.copyList(poordaEntities, PoordaVO.class);
        copy.setLista(poordaVOS);
        return ResponseDTO.ok(copy);
    }

    public ResponseDTO<PoordVO> infoUnexecute(Long id) {


        PoordEntity poordEntity = poordDao.selectById(id);
        PoordVO copy = SmartBeanUtil.copy(poordEntity, PoordVO.class);
        List<PoordaVO> poordaVOS = poordaDao.infoUnexecute(id);
        copy.setLista(poordaVOS);
        return ResponseDTO.ok(copy);

    }
}
